package com.qdu.hyy.DirectorAverageRating;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class DirectorAverageRatingReducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {

    private DoubleWritable averageScore = new DoubleWritable();

    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
        double sum = 0.0;
        int count = 0;
        for (DoubleWritable val : values) {
            sum += val.get();
            count++;
        }
        averageScore.set(sum / count);
        context.write(key, averageScore);
    }
}